package com.ruoyi.common.utils;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Random;

public class AESUtil {


        public static void main(String args[]) throws Exception {
            // app_key
            String cKey = "5D14D863E5F82D8013A73AC768B8C53C";
            // 需要加密的字串
            String cSrc = "0Pax6b10Cd:1632368556:1";
            // app_secret 取前16位;
            String cIv = "5D14D863E5F82D80";
            System.out.println(cSrc);
            // 加密
            long lStart = System.currentTimeMillis();
            String enString = Encrypt(cSrc, cKey, cIv);
            System.out.println("加密后的字串是：" + enString);

            long lUseTime = System.currentTimeMillis() - lStart;
            System.out.println("加密耗时：" + lUseTime + "毫秒");
            // 解密
            lStart = System.currentTimeMillis();
            String DeString = Decrypt(enString, cKey, cIv);
            System.out.println("解密后的字串是：" + DeString);
            lUseTime = System.currentTimeMillis() - lStart;
            System.out.println("解密耗时：" + lUseTime + "毫秒");

        }

        public static String Encrypt(String sSrc, String sKey, String sIv) throws Exception {

            Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding");
            int blockSize = cipher.getBlockSize();

            byte[] dataBytes = sSrc.getBytes();
            int plaintextLength = dataBytes.length;
            if (plaintextLength % blockSize != 0) {
                plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
            }

            byte[] plaintext = new byte[plaintextLength];
            System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);

            SecretKeySpec keyspec = new SecretKeySpec(sKey.getBytes(), "AES");
            IvParameterSpec ivspec = new IvParameterSpec(sIv.getBytes());

            cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
            byte[] encrypted = cipher.doFinal(plaintext);

            return byte2hex(encrypted).toLowerCase();
        }

        public static String Decrypt(String sSrc, String sKey, String sIv) throws Exception {

            byte[] encrypted1      = hex2byte(sSrc);

            Cipher cipher          = Cipher.getInstance("AES/CFB/NoPadding");
            SecretKeySpec keyspec  = new SecretKeySpec(sKey.getBytes(), "AES");
            IvParameterSpec ivspec = new IvParameterSpec(sIv.getBytes());

            cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);

            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original);

            return originalString;
        }

        public static byte[] hex2byte(String strhex) {
            if (strhex == null) {
                return null;
            }
            int l = strhex.length();
            if (l % 2 == 1) {
                return null;
            }
            byte[] b = new byte[l / 2];
            for (int i = 0; i != l / 2; i++) {
                b[i] = (byte) Integer.parseInt(strhex.substring(i * 2, i * 2 + 2),
                        16);
            }

            return b;
        }

        public static String byte2hex(byte[] b) {
            String hs = "";
            String stmp = "";
            for (int n = 0; n < b.length; n++) {
                stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
                if (stmp.length() == 1) {
                    hs = hs + "0" + stmp;
                } else {
                    hs = hs + stmp;
                }
            }

            return hs.toUpperCase();
        }


    //生成固定长度随机数
    public static String getStringRandom(int length) {
        String val = "";
        Random random = new Random();
        //参数length，表示生成几位随机数
        for(int i = 0; i < length; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if( "char".equalsIgnoreCase(charOrNum) ) {
                //输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val += (char)(random.nextInt(26) + temp);
            } else if( "num".equalsIgnoreCase(charOrNum) ) {
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }


}
